<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/head :: header(~{::title},~{::link},~{::style})">
</head>
<body>

<div id="content" class="content">
    <div class="layui-row">
        <div class="layui-col-md2">
            <div class="row">
                <div class="col-sm-12">
                    <div class="well">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md10">
            <div class="row">
                <div class="col-sm-12">
                    <div class="well">
                        <fieldset>
                            <legend>
                                <form class="form-inline" role="form" id="searchForm" name="searchForm"
                                      onkeydown="if(event.keyCode==13){return false;}">
                                    <div class="form-group">
                                        <input type="text" name="keyword" class="form-control table-form-tip" data-toggle="tooltip" data-placement="top" title="关键字"  placeholder="关键字">
                                    </div>
                                    <input type="hidden" id="deptId" name="deptId">
                                    <div class="form-group">
                                        <button id="btn-search" type="button" class="layui-btn layui-btn-radius">查询</button>
                                    </div>
                                </form>
                            </legend>
                            <button type="button" th:each="b : ${button.pageButtons}" th:id="${b.btnId}"
                                    th:class="${b.btnClass}" th:text="${b.title}" style="margin-left: 4px;"></button>
                        </fieldset>
                        <table id="datatable" class="table table-striped table-bordered zy-table-ellipsis"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>

<div th:replace="common/htmlJS::common_js(~{::script})">
<script th:inline="javascript">
    var $table, _table;


    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pid"
            },key: {
                name: "deptName",//name是共通化属性，不是simpleData仅有的，所以与idKey不同，单独用key设置
            }
        },
        callback: {
            onClick: onClick
        }
    };


    $(function () {

        $.ajax({
            type: "Get",
            url: basePath+'/system/dept/list/tree',          //ajax请求地址
            success: function (result) {
                if (result.code==200) {
                    var treeObj = $.fn.zTree.init($("#treeDemo"), setting, result.data);  //加载数据
                    treeObj.expandAll(true);
                }else{
                    layer.msg(result.msg, {icon: 2,shift: 6});
                }
            },
        });

        $table = $('#datatable');
        _table = $table.dataTable(
            $.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
                ajax: function (data, callback, settings) {
                    var param = $("#searchForm").serializeJson();
                    var params = manage.getQueryCondition(data,param);
                    zy.ajax({
                        url: basePath + '/system/user/list',
                        data: params,
                        success: function (result) {
                            if (result.code == 200) {
                                callback(result.data);
                            } else {
                                layer.msg(result.msg, {icon: 2,shift: 6});
                            }
                        }
                    });

                },
                columns: [CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
                    {
                        title: "登录名",
                        data: "username",
                        width: "80"
                    }, {
                        title: "用户名",
                        data: "nickname",
                        width: "80"
                    }, {
                        title: "角色",
                        data: "roleTitle",
                        width: "100",
                        orderable: false
                    }, {
                        title: "所属部门",
                        data: "deptName",
                        width: "150",
                        orderable: false
                    }, {
                        title: "职务",
                        data: "postName",
                        width: "120",
                        orderable: false
                    }, {
                        title: "创建时间",
                        data: "createDate",
                        width: "120",
                        render: CONSTANT.DATA_TABLES.RENDER.DATE_TIME
                    }, {
                        title: "操作",
                        width: "200",
                        orderable: false,
                        render: function (data, type, row, meta) {
                            var tableButtons = [[${button.tableButtons}]];
                            return manage.tableToolBar(tableButtons,row);
                        }
                    }]
            })).api();
        $table.parent().attr('style', 'overflow-x: auto');
        // 行点击事件
        $("tbody", $table).on("click", "tr", function (event) {
            $(this).addClass("active").siblings().removeClass("active");
            var item = _table.row(this).data();
            manage.currentItem = item;

        });
        //复选框
        $table.on("change", ":checkbox", function () {
            if ($(this).is("[name='cb-check-all']")) {
                $(":checkbox", $table).prop("checked", $(this).prop("checked"));
            } else {
                var checkbox = $("tbody :checkbox", $table);
                $(":checkbox[name='cb-check-all']", $table).prop('checked', checkbox.length == checkbox.filter(':checked').length);
            }
        }).on("click", ".td-checkbox", function (event) {
            !$(event.target).is(":checkbox") && $(":checkbox", this).trigger("click");
        });
        //初始化表格搜索框提示
        manage.init();
        //条件搜索
        $("#btn-search").click(function () {
            _table.draw();
        });

        $("#btn-add").click(function () {
            var deptId = $("#deptId").val();
            addInit(deptId)
        });


    });

    function addInit(deptId){
        zy.openPage('新增用户','/system/user/add/page?deptId='+deptId);
    }

    function edit(row) {
        if(row.roleName=='superAdmin' || row.roleName=='admin'){
            layer.msg('管理员不能编辑', {icon: 2,shift: 6});
            return;
        }
        zy.openPage('编辑用户','/system/user/edit/page/'+row.id);
    }
    function detail(row) {
        zy.openPage('用户详情',"/system/user/detail/page/"+row.id);
    }

    function del(row) {
        if(row.roleName=='superAdmin' || row.roleName=='admin'){
            layer.msg('管理员不能删除', {icon: 2,shift: 6});
            return;
        }
        layer.confirm('确定要删除吗', {
            icon : 3
        }, function() {
            zy.ajax({
                url : basePath+'/system/user/status/delete',
                data : {ids:row.id},
                success : function(result) {
                    if (result.code==200) {
                        _table.draw(false);
                        layer.msg(result.msg);
                    }else{
                        layer.msg(result.msg, {icon: 2,shift: 6});
                    }
                }
            });


        });

    }

    function onClick(event, treeId, treeNode, clickFlag) {
        $("#deptId").val(treeNode.id)
        _table.draw();
    }

    function resetPassword(row){
        window.layerIndex = layer.open({
            type: 2,
            title: '研判工作表',
            move: false,
            area: ['40%', '60%'],
            content: '/system/user/resetPassword/page/'+row.id,
            success: function (layero, addIndex) {
                setTimeout(function () {
                    layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500);
            }
        });
        window.onresize = function(){
            layer.style(layerIndex, {
                width: $('#main').width()+'px'
            });
        }

    }

</script>
</div>
</body>
</html>